<?php

/**
* 游戏充值、登录函数库
*
* 登录插件函数
    function 数据库游戏标识_login_fun($user = array(), $game = array(), $server = array()){
        $login_args = 'uid=2342&name=test&token=sdf35saagasdf345343434';
        return $login_args;
    }

* 充值插件函数
    function 数据库游戏标识_pay_fun($order = array(), $game = array(), $server = array(), $debug = false){
        $pay_args = 'uid=2342&name=test&token=sdf35saagasdf345343434';
        return $pay_args;
    }
*/

function wly_login_fun($user, $game, $server){
    $sno    = 'wly'.($server['server_no'] + 1);
    $uid    = $user['user_id'];
    $time   = time();
    $sign   = md5($sno.$uid.$time.$game['game_login_key']);
    return "sno=$sno&u=$uid&t=$time&sign=$sign";
}

function wly_pay_fun($order, $game, $server, $debug = false){
    $sno    = 'wly'.($server['server_no'] + 1);
    $uid    = $order['user_id'];
    $time   = time();
    $order_no   = $order['order_no'];
    $amount     = $order['order_real_amount'];
    $sign = md5($sno.$amount.$uid.$order_no.$time.$game['game_pay_key']);
    return "sno=$sno&amount=$amount&u=$uid&order_no=$order_no&time=$time&sign=$sign";
}

/**
* 登录游戏接口方法
* @param user array 用户数据 array('user_id'=>'', 'user_code'=>'', 'user_identity'=>);
* @params game array 游戏数据
* @param server array 游戏服务区数据
* @param debug bool 调试
* return void
*/
function play_game_fun($user, $game, $server, $debug = false){
    global $tpl;
    $time = time();
    $login_md5  = '';
    $client_ip = getClientIp();
    // 替换标识
    if($game['game_sno_format']){
            $server['server_no'] =  sprintf('%0'.$game['game_sno_format'].'d', $server['server_no']);
    }
    $game_login_url = str_replace('{sno}', $server['server_no'], $game['game_login_url']);
    if($game['game_login_plugin'] == 1) {
        $fun = $game['game_code'].'_login_fun'; // 调用进入游戏插件函数
        $game_login_args = $fun($user, $game, $server);
    }else{
    	$ost = urlencode($game['game_site_url']);
        $find     = array('{uid}', '{uname}', '{sno}', '{time}', '{sid}', '{cm}', '{key}', '{clientip}', '{ncm}','{channel}','{ost}');
        $replace = array($user['user_id'], $user['user_code'], $server['server_no'], $time, $server['server_id'], $user['cm'], $game['game_login_key'], $client_ip, $user['cm'] == 1 ? 0 : 1,$user['user_channel'],$ost);
        $md5_args = str_replace($find, $replace, $game['game_login_md5']);
        $login_md5 =  md5($md5_args);
        array_push($find,'{server_name}');array_push($replace,urlencode($server['server_name']));
        $game_login_args = str_replace($find, $replace, $game['game_login_args']);
        $game_login_args = str_replace('{md5}', $login_md5, $game_login_args);
    }
    $method = strtoupper($game['game_login_method']);

    //返回调试数据
    if($debug){
        return array(
            'md5_args'  => $md5_args,
            'login_args'=> $game_login_args,
            'login_url' => $game_login_url.'?'.$game_login_args
            );
    }
    if($method == "POST") {
        $tmp = explode('&', $game_login_args);
        $data = array();
        foreach($tmp as $row){
            list($name, $value) = explode('=', $row);
            $data[$name] = $value;
        }
        $tpl->assign('charset', 'utf-8');
        $tpl->assign('request_url', $game_login_url);
        $tpl->assign('post_data', $data);
        $tpl->assign('button_value', '点击进入游戏');
        $tpl->display('post.data.html');
        exit;
    }else{
        $location = $game_login_url.'?'.$game_login_args;
        redirect($location);exit;
    }
}

/**
* 充值到游戏 #订单支付成功后调用此函数
* @param order array 订单数据
* @params debug bool default false 调试
* return bool 成功返回TRUE 失败返回false 调试状态返回调试信息
*/
function pay_to_game($order, $debug = false){

        // 验证订单数据
        if(!$order || $order['order_status'] == 0 || $order['order_amount'] == 0 || $order['order_type'] == 1){
            return false;
        }

        $gameModel = loadModel('g5_game');
        $game = $gameModel->getOne('*', array('game_id'=>$order['game_id']));
        $server = array('server_id'=>$order['server_id'], 'server_no'=>$order['server_no']);
        $clientip = getClientIP();
        $to_game_amount = intval($order['order_real_amount']);
        $to_game_point = $order['point'] = intval($order['order_real_amount'] * $game['game_money_ratio']);
        $time = time();
        $client_ip = getClientIp();

        // 处理充值接口标识
        if($game['game_sno_format']){
            $server['server_no'] =  sprintf('%0'.$game['game_sno_format'], $server['server_no']);
        }
        $game_pay_url = str_replace('{sno}', $server['server_no'], $game['game_pay_url']);

        $md5_args = $game_pay_args = '';
        if($game['game_pay_plugin'] == 1) {
            $fun = $game['game_code'].'_pay_fun'; // 调用插件自定义函数
            $game_pay_args = $fun($order, $game, $server, $debug);
        }else{
            $find = array('{uid}', '{uname}', '{orderno}', '{point}', '{amount}','{time}', '{key}','{clientip}','{sno}', '{sid}');
            $replace = array($order['user_id'], $order['user_code'], $order['order_no'], $to_game_point, $to_game_amount, $time, $game['game_pay_key'], $client_ip, $server['server_no'],$server['server_id']);
            $md5_args = str_replace($find, $replace, $game['game_pay_md5']);
            $md5 =  md5($md5_args);
            $find[] = '{md5}';
            $replace[] = $md5;
            $game_pay_args = str_replace($find, $replace, $game['game_pay_args']);
        }

        // 支付接口请求方式
        $method = strtoupper($game['game_pay_method']);
        if($method == "GET") {
            $location = $game_pay_url.'?'.$game_pay_args;
            $result = curl_get_contents($location);
        }elseif($method == "POST") {
            $tmp = explode('&', $game_paya_args);
            $data = array();
            foreach($tmp as $row){
                list($name, $value) = explode('=', $row);
                $data[$name] = $value;
            }
            $result = curl_get_contents($location, 'POST', $data);
        }elseif($method == "SHELL") {
            $location = $game_pay_url.'?'.$game_pay_args;
            $tmpfile    =    tempnam("/tmp","FOO");
            $cmd    =    "wget --timeout=10 --tries=1 -O $tmpfile \"".$location."\"";
            exec($cmd);
            $http = file($tmpfile);
            $xmlstr = $http[0];
        }else if($method == 'SOAP') {
            $tmp = explode('&', $game_paya_args);
            $data = array();
            foreach($tmp as $row){
                list($name, $value) = explode('=', $row);
                $data[$name] = $value;
            }
            $soap_client = new SoapClient($game_pay_url) ;
            $soap_result = $soap_client->__soapCall(trim($game['game_pay_soap_fun']), $data);
            $result = $soap_result->out;
        }else {
            $location = $game_pay_url.$game_pay_args;
            $result = curl_get_contents($location);
        }

        // 处理返回结果,结果类型 json xml string
        $out_type = strtoupper($game['game_pay_return_type']);
        $return_string = trim($game['game_pay_return']);
        $result = trim($result);

        // 调试数据
        $return = array('status' => false, 'msg'=>array());
        if($debug){
            $return['msg'] = array(
                'md5_args'      => $md5_args,
                'pay_args'      => $game_pay_args,
                'pay_url'       => $game_pay_url.'?'.$game_pay_args,
                'pay_result'    => $result
                );
        }

        // 返回充值结果
        list($success, $error) = explode('|', $return_string);
        if($out_type == 'JSON'){
            list($param, $value) = explode('=', $success);
            $json = json_decode($result);
            if($json->$param == $value){
                $return['status'] = true;
            }
        }elseif($out_type == 'XML'){
            if(preg_match("/".$success."/i",$result)) {
                $return['status'] = true;
            }
        }else{
            // 默认string
            if($result !==false && $result == $success){
                $return['status'] = true;
            }
        }
        return $return;
}